package com.youmu.sichuansfc.service.impl;

import com.youmu.sichuansfc.dao.CompanyDao;
import com.youmu.sichuansfc.dao.CompanyRiskDao;
import com.youmu.sichuansfc.domain.YoumuCompany;
import com.youmu.sichuansfc.domain.YoumuCompanyRisk;
import com.youmu.sichuansfc.domain.pojo.AreaPojo;
import com.youmu.sichuansfc.domain.pojo.CompanyCountPojo;
import com.youmu.sichuansfc.dto.req.CompanyDto;
import com.youmu.sichuansfc.dto.req.CompanyRiskDto;
import com.youmu.sichuansfc.dto.rsp.*;
import com.youmu.sichuansfc.service.AreaService;
import com.youmu.sichuansfc.service.CompanyService;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {


    @Autowired
    CompanyDao companyDao;
    @Autowired
    CompanyRiskDao companyRiskDao;
    @Autowired
    AreaService areaService;

    private Logger logger = Logger.getLogger(this.getClass());
    @Override
    public YoumuCompany insert(CompanyDto companyDto) {

        YoumuCompany youmuCompany = new YoumuCompany();
        if(companyDto.getIsDelete() != null && companyDto.getIsDelete()){
            youmuCompany = companyDao.findById(companyDto.getId());
            youmuCompany.setIsDelete(true);
            youmuCompany = companyDao.insert(youmuCompany);
            return  youmuCompany;
        }else{
            BeanUtils.copyProperties( companyDto, youmuCompany);
        }
        youmuCompany.setAddTime(new Timestamp(new Date().getTime()));
        //从阿里云获取企业详细信息
        if(youmuCompany.getDetail()== null || "".equals(youmuCompany.getDetail())){
            youmuCompany.setDetail("");
        }
        youmuCompany.setIsDelete(false);
        youmuCompany = companyDao.insert(youmuCompany);

        System.out.println("---------公司添加end-------------");
        //上传风险提示
            List<CompanyRiskDto> companyRiskDtoList = companyDto.getCompanyRiskDtoList();
            //for (int i = 0 ; i < companyRiskDtoList.size(); i++){
            for (CompanyRiskDto companyRiskDto : companyRiskDtoList){
//                String problemCode = ((Map) companyRiskDtoList.get(i)).get("problemCode").toString();
//                String cue = ((Map) companyRiskDtoList.get(i)).get("cue").toString();
//                String detail = ((Map) companyRiskDtoList.get(i)).get("detail").toString();
//                String rank = ((Map) companyRiskDtoList.get(i)).get("rank").toString();
//                String img = ((Map) companyRiskDtoList.get(i)).get("img").toString();
//                String suggest = ((Map) companyRiskDtoList.get(i)).get("suggest").toString();
                String problemCode = companyRiskDto.getProblemCode();
                String cue = companyRiskDto.getCue();
                String detail = companyRiskDto.getDetail();
                String rank = companyRiskDto.getRank();
                String img = companyRiskDto.getImg();
                String suggest = companyRiskDto.getSuggest();
                Long id = null;
                try {
                    id = companyRiskDto.getId();
                }catch (Exception e){

                }

                YoumuCompanyRisk youmuCompanyRisk = new YoumuCompanyRisk();

                youmuCompanyRisk.setRank(rank);
                youmuCompanyRisk.setDetail(detail);
                youmuCompanyRisk.setCue(cue);
                youmuCompanyRisk.setImg(img);
                System.out.println("------------------------------------");
                System.out.println(img);
                System.out.println("------------------------------------");
                youmuCompanyRisk.setProblemCode(problemCode);
                youmuCompanyRisk.setAddTime(new Timestamp(new Date().getTime()));
                youmuCompanyRisk.setCompanyId(youmuCompany.getId().longValue());
                youmuCompanyRisk.setIsDelete(false);
                if(id != null && !"".equals(id)) {
                    youmuCompanyRisk.setId(id);
                }
                System.out.println("------------------------------------");
                System.out.println(new JSONObject(youmuCompanyRisk) );
                System.out.println("------------------------------------");
                companyRiskDao.update(youmuCompanyRisk);
            }
        return youmuCompany;
    }

    @Override
    public CompanyT searchCompany(Timestamp startTime, Timestamp endTime, String companyName,
                                  Integer companyStatus, int num, int size) {

        List<SearchCompany> searchCompanyList = new ArrayList<>();
        CompanyT companyT = companyDao.findBy(startTime, endTime, companyName, companyStatus, num, size);


        // 拼装公司信息
        List<YoumuCompany> youmuCompanies = companyT.getYoumuCompanyList();
        if (youmuCompanies != null && youmuCompanies.size() > 0){
            for (YoumuCompany youmuCompany : youmuCompanies){
                SearchCompany searchCompany = new SearchCompany();
                BeanUtils.copyProperties(youmuCompany,searchCompany);

                // 查询该公司风险信息
                List<YoumuCompanyRisk> youmuCompanyRiskList = companyRiskDao.findByCompanyId(youmuCompany.getId());
                String riskCue = "";
                if(youmuCompanyRiskList != null && youmuCompanyRiskList.size() > 0){
                    riskCue += youmuCompanyRiskList.get(0).getCue() +" ";
                }
                searchCompany.setRiskCue(riskCue);
                searchCompanyList.add(searchCompany);
            }
        }
        // 回参
        CompanyT companyT1 = new CompanyT();
        companyT1.setCompanyCount(companyT.getCompanyCount());
        companyT1.setYoumuCompanyList(searchCompanyList);
        return companyT1;
    }

    @Override
    public CompanyDetail companyDetail(Long id) {

        // check null
        if(id == null)
            return  null;
        //查询公司基本信息
        YoumuCompany youmuCompany = companyDao.findById(id);

        // 查询公司风险信息
        List<YoumuCompanyRisk> youmuCompanyRiskList = companyRiskDao.findByCompanyId(youmuCompany.getId());

        //拼接数据
        CompanyDetail companyDetail = new CompanyDetail();
        BeanUtils.copyProperties(youmuCompany,companyDetail);
        companyDetail.setCompanyRiskList(youmuCompanyRiskList);
        return companyDetail;
    }

    @Override
    public YoumuCompanyRisk companyRiskDetail(Long id) {
        if(id == null)
            return null;

        // 查询该公司风险信息
        YoumuCompanyRisk youmuCompanyRiskList = companyRiskDao.findById(id);

        return youmuCompanyRiskList;
    }

    @Override
    public CompanyTypeCount companyTypeCount() {

        CompanyTypeCount companyTypeCount = new CompanyTypeCount();
        Integer grayCompanys = companyDao.findByCompanyType(3).size();
        Integer blackCompanys = companyDao.findByCompanyType(4).size();
        companyTypeCount.setBlackCompany(blackCompanys);
        companyTypeCount.setGray(grayCompanys);

        return companyTypeCount;
    }

    @Override
    public int errCompany() {
        return companyDao.errorCompany();
    }

    @Override
    public List<CompanyCountPojo> CompanyTypestatistic(int SQLKEY, Long areaId) {

        List<Long> areaidList = new ArrayList<>();
        Object[] areaIds = null;
        List<AreaPojo> areaPojos = areaService.findAllAreaByParentId(areaId);

        List<CompanyCountPojo> companyCountPojos = companyDao.findByType(SQLKEY, areaPojos);

        return companyCountPojos;
    }

    @Override
    public List<CompanyAreaStatisticDto> CompanyAreaStatistic(Long areaId) {
        List<CompanyAreaStatisticDto> companyAreaStatisticDtoList = new ArrayList<>();

        // 查询 areaId 的下一级城市
        List<AreaPojo> areaParents = areaService.findArea(areaId);
        if(areaParents != null && areaParents.size() > 0){
            for (AreaPojo areaParent : areaParents){
                CompanyAreaStatisticDto companyAreaStatisticDto = new CompanyAreaStatisticDto();
                companyAreaStatisticDto.setAreaId(areaParent.getId());
                companyAreaStatisticDto.setAreaName(areaParent.getAreaName());
                // 查询 该areaId 下所有等级城市
                List<AreaPojo> areaPojos = areaService.findAllAreaByParentId(areaParent.getId());
                // 查询 company
                List<CompanyCountPojo> companyCountPojos = companyDao.findByType(null, areaPojos);
                companyAreaStatisticDto.setCount(companyCountPojos != null && companyCountPojos.size() > 0  ? companyCountPojos.get(0).getCount() : 0);

                companyAreaStatisticDtoList.add(companyAreaStatisticDto);
            }
        }

        return companyAreaStatisticDtoList;
    }
}
